<template>
  <a-layout id="components-layout-demo-custom-trigger">
    <a-layout-sider v-model="collapsed"
                    :trigger="null"
                    collapsible>
      <div class="logo" />
      <a-menu theme="dark"
              mode="inline"
              :default-selected-keys="[$route.path]"
              @click="
              ({key}) => {
                this.$router.push(key)
              }">
        <a-menu-item v-for="nav in navs"
                     :key="nav.path">
          <a-icon :type="nav.icon" />
          <span>{{nav.name}}</span>
        </a-menu-item>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0">
        <a-icon class="trigger"
                :type="collapsed ? 'menu-unfold' : 'menu-fold'"
                @click="() => (collapsed = !collapsed)" />
        <div style="cursor: pointer;float: right;marginRight: 40px; display: flex;alignItems: center "
             className="userInfo">

          <a-dropdown>
            <div class="ant-dropdown-link">
              <a-avatar :src="userInfo.avatar"></a-avatar>
              <span>{{userInfo.nickName}}</span>
            </div>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;">个人资料</a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:;">消息中心</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="logout"
                   href="javascript:;">退出登录</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </div>
      </a-layout-header>
      <a-layout-content :style="{ margin: '24px 16px', padding: '24px', background: '#fff', minHeight: '280px' }">
        <router-view />
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>
<script>
// const userInfo = localStorage.getItem('userInfo')
//   ? JSON.parse(localStorage.getItem('userInfo'))
//   : {}
export default {
  data () {
    return {
      collapsed: false,
      userInfo: localStorage.getItem('userInfo')
        ? JSON.parse(localStorage.getItem('userInfo'))
        : {},
      navs: ''
    }
  },
  created () {
    this.getRole()
  },
  methods: {
    // beforeRouteEnter (to, from, next) {
    //   console.log(to.meta.roles)
    //   console.log(this.userInfo.role)
    //   if (to.meta.roles.includes(this.userInfo.role)) {
    //     next()
    //   } else {
    //     next('/nopermission')
    //   }
    // },
    getRole () {
      this.navs = this.$router.options.routes[1].children.filter(nav => nav.meta.isNav && nav.meta.roles.includes(this.userInfo.role))
    },
    logout () {
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      this.$router.go(0)
    }
  }
}
</script>
<style>
#components-layout-demo-custom-trigger .trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}

#components-layout-demo-custom-trigger .trigger:hover {
  color: #1890ff;
}

#components-layout-demo-custom-trigger .logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px;
}
</style>
